import {
  IModel,
  SPI,
  TableWidget,
  BaseElementWidget,
  getModelByUrl,
  getModel,
  Widget,
  queryPage,
  queryOne,
  CustomWidget,
  http,
  IModelField,
  insertOne,
  buildSingleItemParam,
  ModelFieldType,
  callFunction,
  callFun
} from '@kunlun/dependencies';
import HomeConfigWidget from './HomeConfig.vue';
@SPI.ClassFactory(CustomWidget.Token({ widget: 'custom-home-config' }))
export class CustomHomeConfigWidget extends TableWidget {
  public initialize(props) {
    super.initialize(props);
    this.setComponent(HomeConfigWidget);
    return this;
  }

  @Widget.Reactive()
  protected configData: Record<string, unknown>[] = [];

  protected async mounted() {
    super.mounted();

    this.loadData();
  }

  // 请求首页栏目数据
  @Widget.Method()
  public async loadData() {
    const data = await queryPage(this.model.model, { pageSize: 15, currentPage: 1 }, undefined, {
      scene: 'DemoMenus_ScuMenu_ScuColumnEntityMenu'
    });
    console.log('loadData', data.content);
    this.configData = [];
    data.content.forEach((value, index, array) => {
      this.loadColumnContent(value);
    });
  }

  @Widget.Method()
  public async loadColumnContent(value) {
    console.log('loadColumnContent', value.id);
    const query = `
        query{
            scuColumnElementEntityQuery{
                    queryColumn(columnId:"${value.id}"){
                        id
        elementName
        columnType
        picUrl
        weight
        jumpType
        dataMode
        jumpUrl
        categoryIds
        detailId
        studentCount
        lecturerName
        lecturerCompany
        lecturerPosition
        paymentType
        originalPrice
        currentPrice
        functionModule
        articlePicType
        treasureChestSource
        treasureChestContent
        treasureChestTags
        columnId
        columnEntity {
          id
          columnName
        }
        createDate
        writeDate
        createUid
        writeUid
                    }
            }
        }`;
    const result = await http.query('DemoCore', query);
    const content = result.data.scuColumnElementEntityQuery.queryColumn;
    console.log('loadColumnContent', content);
    const columnContent = content; // 栏目下的内容
    value.columnContent = columnContent;
    value.isShowRight = false;
    if (value.columnStyle === 1) {
      value.widgetName = 'QxbColumnKongList';
    } else if (value.columnStyle === 2) {
      // 图片轮播
      value.widgetName = 'QxbBannerWidget';
    } else if (value.columnStyle === 6) {
      // 标题图片横版（二）
      // 标题图片横版（二）
      if (value.weight) {
        console.log('value11111---', value);
        value.widgetName = 'QxbColumnListWidget';
      } else {
        value.widgetName = 'QxbColumnListWidget2';
      }
      // value.widgetName = "QxbColumnListWidget"
    } else if (value.columnStyle === 1) {
      // 金刚区
      value.widgetName = 'KongListFieldWidget';
    } else {
      value.widgetName = 'QxbBannerWidget';
    }
    this.configData.push(value);
    console.log('configData', this.configData);
  }

  /**
   * 创建栏目
   */
  @Widget.Method()
  public async createColumn(data) {
    console.log('createColumn', 'data-createColumn', data);
    const result = await callFun(
      'demo.ScuColumnEntity',
      {
        method: 'create',
        name: 'create',
        type: 'CREATE',
        fun: 'create',
        argumentList: [{ name: 'data', ttype: ModelFieldType.ManyToOne }]
      },
      data
    );
    console.log('createColumn result', result);

    this.loadData();
  }

  /**
   * 修改栏目
   */
  @Widget.Method()
  public async modifyColumn(data) {
    console.log('modifyColumn', 'data', data);
    const result = await callFun(
      'demo.ScuColumnEntity',
      {
        method: 'update',
        name: 'update',
        type: 'UPDATE',
        fun: 'update',
        argumentList: [{ name: 'data', ttype: ModelFieldType.ManyToOne }]
      },
      data
    );
    console.log('modifyColumn result', result);

    this.loadData();
  }

  /**
   * 删除栏目
   */
  @Widget.Method()
  public async deleteColumn(dataList) {
    console.log('deleteColumn', 'dataList', dataList);
    const result = await callFun(
      'demo.ScuColumnEntity',
      {
        method: 'delete',
        name: 'delete',
        type: 'DELETE',
        fun: 'delete',
        argumentList: [{ name: 'dataList', ttype: ModelFieldType.ManyToOne }]
      },
      dataList
    );
    console.log('deleteColumn result', result);

    this.loadData();
  }

  /**
   * 创建栏目内容
   */
  @Widget.Method()
  public async createColumnContent(data) {
    console.log('createColumnContent', 'data', data);
    const result = await callFun(
      'demo.ScuColumnElementEntity',
      {
        method: 'create',
        name: 'create',
        type: 'CREATE',
        fun: 'create',
        argumentList: [{ name: 'data', ttype: ModelFieldType.ManyToOne }]
      },
      data
    );
    console.log('createColumnContent result', result);

    this.loadData();
  }

  /**
   * 修改栏目内容
   */
  @Widget.Method()
  public async modifyColumnContent(data) {
    console.log('modifyColumnContent', 'data', data);
    const result = await callFun(
      'demo.ScuColumnElementEntity',
      {
        method: 'update',
        name: 'update',
        type: 'UPDATE',
        fun: 'update',
        argumentList: [{ name: 'data', ttype: ModelFieldType.ManyToOne }]
      },
      data
    );
    console.log('modifyColumnContent result', result);

    this.loadData();
  }
}
